package com.ai.nsa.utils;

import com.ai.nsa.NSAException;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Feng Chen
 */
public final class MapTools {

    /**
     * 缺省日期格式
     */
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String getString(Map<String, Object> param, String key) {
        return getString(param, key, null);
    }

    public static String getString(Map<String, Object> param, String key, String defaultValue) {
        Object value = param.get(key);
        if (value == null) {
            return defaultValue;
        } else {
            return value.toString();
        }
    }

    public static Integer getInteger(Map<String, Object> param, String key, Integer defaultValue) {
        Integer value = getIntegerObject(param, key);
        return value == null ? defaultValue : value;
    }

    public static Integer getInteger(Map<String, Object> param, String key) {
        return getInteger(param, key, 0);
    }


    public static Long getLong(Map<String, Object> param, String key, Long defaultValue) {
        Long value = getLongObject(param, key);
        return value == null ? defaultValue : value;
    }


    public static Long getLong(Map<String, Object> param, String key) {
        return getLong(param, key, 0L);
    }

    public static Date getDate(Map<String, Object> param, String key, String pattern) {
        Object value = param.get(key);
        if (value == null) {
            return null;
        } else if (value instanceof String) {
            try {
                return DateUtils.parseDate((String) value, new String[]{pattern});
            } catch (ParseException e) {
                throw new NSAException(e);
            }
        } else if (value instanceof Double) {
            return new Date(((Double) value).longValue());
        } else if (value instanceof Date) {
            return (Date) value;
        } else {
            try {
                return DateUtils.parseDate(value.toString(), new String[]{pattern});
            } catch (ParseException e) {
                throw new NSAException(e);
            }
        }
    }

    public static Date getDate(Map<String, Object> param, String key) {
        return getDate(param, key, DEFAULT_DATE_PATTERN);
    }


    public static Boolean getBoolean(Map<String, Object> param, String key) {
        String value = getString(param, key);
        return value != null && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) ?
                Boolean.TRUE : Boolean.FALSE;
    }


    // inner helper

    private static Integer getIntegerObject(Map<String, Object> param, String key) {
        Object value = param.get(key);
        if (value == null) {
            return null;
        } else if (value instanceof String) {
            return Integer.parseInt((String) value);
        } else if (value instanceof Double) {
            return ((Double) value).intValue();
        } else if (value instanceof Date) {
            return (int) ((Date) value).getTime();
        } else {
            return Integer.parseInt(value.toString());
        }
    }

    public static Long getLongObject(Map<String, Object> param, String key) {
        Object value = param.get(key);
        if (value == null) {
            return null;
        } else if (value instanceof String) {
            return Long.parseLong((String) value);
        } else if (value instanceof Double) {
            return ((Double) value).longValue();
        } else if (value instanceof Date) {
            return ((Date) value).getTime();
        } else {
            return Long.parseLong(value.toString());
        }
    }

    public static Map<String, String> objToString(Map<String, Object> m) {
        HashMap<String, String> n = new HashMap<String, String>();

        if (null == m || m.isEmpty()) {
            return null;
        }

        for (String s : m.keySet()) {
            n.put(s, String.valueOf(m.get(s)));
        }
        return n;
    }

}
